<template>
  <!-- 搜索工作栏 -->
  <ContentWrap ref="form">
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="专业名称" prop="majorName">
        <el-input
          v-model.tirm="queryParams.majorName"
          placeholder="请输入专业名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="专业编码" prop="majorCode">
        <el-input
          v-model.tirm="queryParams.majorCode"
          placeholder="请输入专业编码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="学校名称" prop="schoolId">
        <el-select
          v-model.trim="queryParams.schoolId"
          clearable
          filterable
          placeholder="请选择学校"
          @change="changeSchool"
          class="!w-240px"
        >
          <el-option
            v-for="item in schoolList"
            :key="item.id"
            :label="item.schoolName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="机构名称" prop="collegeId">
        <el-select
          v-model.trim="queryParams.collegeId"
          clearable
          filterable
          placeholder="请选择机构"
          @keyup.enter="handleQuery"
          class="!w-240px"
        >
          <el-option
            v-for="item in collegeList"
            :key="item.id"
            :label="item.collegeName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button type="primary" @click="openForm('create')" v-hasPermi="['cmis:major:create']">
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cmis:major:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button type="danger" @click="handleDeleteMulti" v-hasPermi="['cmis:major:delete']">
          <Icon icon="ep:delete" class="mr-5px" /> 批量删除
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap :setPadding="false">
    <el-table
      v-loading="loading"
      :data="list"
      @selection-change="selectionChange"
      stripe
      :max-height="tableHeight"
      >>
      <el-table-column type="selection" width="55" />
      <el-table-column label="专业名称" prop="majorName" show-overflow-tooltip />
      <el-table-column label="专业编码" prop="majorCode" show-overflow-tooltip />
      <el-table-column label="学校名称" prop="schoolName" show-overflow-tooltip />
      <el-table-column label="学院名称" prop="collegeName" show-overflow-tooltip />
      <el-table-column
        label="创建时间"
        prop="createTime"
        :formatter="dateFormatter"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column label="操作" fixed="right" width="100">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cmis:major:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cmis:major:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MajorForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Major">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as MajorApi from '@/api/cmis/major'
import * as SchoolApi from '@/api/cmis/school'
import * as CollegeApi from '@/api/cmis/college'
import MajorForm from './MajorForm.vue'
const form = ref(null)
const { width, height } = useElementSize(form)
const tableHeight = useWindowSize(78 + 76, height)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  majorName: null,
  majorCode: null,
  schoolId: null,
  collegeId: null
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const schoolList = ref([]) //学校列表
const collegeList = ref([]) //机构列表

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await MajorApi.getMajorPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MajorApi.deleteMajor(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await MajorApi.exportMajor(queryParams)
    download.excel(data, '专业.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const changeSchool = async (value: any) => {
  queryParams.collegeId = null
  getCollegeList(value)
}

const getCollegeList = async (schoolId: string) => {
  const res = await CollegeApi.getCollegeList({ schoolId })
  collegeList.value = res
}

const listOfIds = ref([]) //选中的设备

// 选中设备
const selectionChange = (val: any) => {
  listOfIds.value = val.map((item: any) => item.id)
}

// 批量删除操作
const handleDeleteMulti = async () => {
  if (listOfIds.value.length > 0) {
    try {
      // 批量删除的二次确认
      await message.delConfirm()
      // 发起批量删除
      await MajorApi.batchDeleteMajor(listOfIds.value)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  } else {
    message.warning(t('请选择要删除的专业!'))
  }
}

/** 初始化 **/
onMounted(async () => {
  getList()
  // 加载学校列表
  schoolList.value = await SchoolApi.getSchoolList('')
  getCollegeList('')
})
</script>
